package com.jjmmbb.jxSudoku;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.adb.dao.GameDataManager;
import com.adb.entity.GameData;
import com.adb.util.Util;

/**
 * 打开本页面前需要初始化comm.currentAVData和comm.currentGameData
 * 
 * @author yizhe
 * @date 2012-7-13
 */
public class FinishShowActivity extends BaseActivity implements OnClickListener {
	GameData currentGameData = Comm.currentGameData;

	EditText tv_gamedata;
	TextView tv_time_best;
	Button bt_clear;
	Button bt_share;

	// av_info
	TextView av_name, av_birthday, av_height, av_bwh, av_weight,
			av_name_english, av_introduction, av_comment;
	TextView av_birthplace, av_blood_type, av_interest, av_posture,
			av_love_first_taste;

	StringBuffer f_data_finish;

	long ad_js = 0; // 单击广告时的计时,用于模拟双击

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.finish_show);

		// 美女信息
		av_name = (TextView) findViewById(R.id.av_name);
		av_name_english = (TextView) findViewById(R.id.av_name_english);
		av_birthday = (TextView) findViewById(R.id.av_birthday);
		av_height = (TextView) findViewById(R.id.av_height);
		av_bwh = (TextView) findViewById(R.id.av_bwh);
		av_weight = (TextView) findViewById(R.id.av_weight);
		av_birthplace = (TextView) findViewById(R.id.av_birthplace);
		av_blood_type = (TextView) findViewById(R.id.av_blood_type);
		av_interest = (TextView) findViewById(R.id.av_interest);
		av_posture = (TextView) findViewById(R.id.av_posture);
		av_love_first_taste = (TextView) findViewById(R.id.av_love_first_taste);
		av_introduction = (TextView) findViewById(R.id.av_introduction);
		av_comment = (TextView) findViewById(R.id.av_comment);

		// 数独信息
		tv_gamedata = (EditText) findViewById(R.id.f_gamedata);
		tv_time_best = (TextView) findViewById(R.id.f_tv_time_best);
		bt_clear = (Button) findViewById(R.id.f_bt_clear);
		bt_share = (Button) findViewById(R.id.f_bt_share);

		bt_clear.setOnClickListener(this);
		bt_share.setOnClickListener(this);

		ini();
		setNightMode();

		if (Comm.isPure) {
			av_name.setVisibility(View.GONE);
			av_name_english.setVisibility(View.GONE);
			av_birthday.setVisibility(View.GONE);
			av_height.setVisibility(View.GONE);
			av_bwh.setVisibility(View.GONE);
			av_weight.setVisibility(View.GONE);
			av_birthplace.setVisibility(View.GONE);
			av_blood_type.setVisibility(View.GONE);
			av_interest.setVisibility(View.GONE);
			av_posture.setVisibility(View.GONE);
			av_love_first_taste.setVisibility(View.GONE);
			av_introduction.setVisibility(View.GONE);
			av_comment.setVisibility(View.GONE);
		}
	}

	public void ini() {
		// 加载数独信息
		String data_finish = currentGameData.getData_finish(); // 进行中的数据
		String[] temp = new String[81];
		if (81 != data_finish.length()) {
			for (int i = 0; i < 81; i++) {
				temp[i] = "0";
			}
		}
		// data_finish =
		// "016090005052600800300105020008010350023060009100039002030026070265700000904350000";
		f_data_finish = new StringBuffer();
		f_data_finish.append(res.getString(R.string.app_name));
		f_data_finish.append("第");
		f_data_finish.append(currentGameData.getAv_id());
		f_data_finish.append("关\n");
		if (data_finish.length() >= 81) {
			for (int i = 0; i < 81; i++) {
				temp[i] = data_finish.charAt(i) + "";
				f_data_finish.append(data_finish.charAt(i));
				if (i % 9 == 8) {
					f_data_finish.append(",\n");
				} else {
					f_data_finish.append(",");
				}
			}
		}

		tv_gamedata.setText(f_data_finish);

		int best = currentGameData.getTime_best();
		tv_time_best.setText("最佳记录：" + Util.secondFormat(best));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.f_bt_clear:
			currentGameData.setTime_best(0);
			GameDataManager gdm = new GameDataManager();
			gdm.update(this, currentGameData);
			tv_time_best.setText("最佳记录 : 0:00");
			break;

		case R.id.f_bt_share:
			share();
			break;
		}
	}

	/**
	 * 分享到微博等
	 */
	void share() {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain"); // 纯文本

		// 组装文本
		StringBuffer sb = f_data_finish;

		int best = currentGameData.getTime_best();
		f_data_finish.append("最佳记录：").append(Util.secondFormat(best));
		f_data_finish.append("\nhttp://jjmmbb.com");

		intent.putExtra(Intent.EXTRA_SUBJECT, "");
		intent.putExtra(Intent.EXTRA_TEXT, sb.toString());
		startActivity(Intent.createChooser(intent, getTitle()));
	}

}
